import Jsw from "../../assets/js/Jsw.js";

let session;

// 初始化并获得设备列表信息
const init = () => {
  return new Promise((resolve) => {
    Jsw.then(() => {
      jSW = window.jSW;
      console.log("---------- 这是heimet.js里的init() ---------- ");
      jSW.swInit({
        url: "http://153.99.98.91:8081",
        calltype: jSW.CallProtoType.AUTO,
        oninit() {
          if (session) {
            session = null;
          }
          session = new jSW.SWSession({
            server: "153.99.98.91",
            port: 9701,
            onopen: function(sess) {
              sess.swLogin({
                user: "admin",
                password: "123456",
              });
            },
          });

          // 注册事件的回调函数
          //   session.swAddCallBack("login", sessionCallback);
          //   session.swAddCallBack("logout", sessionCallback);
          session.swAddCallBack("pulist", function() {
            resolve(session);
          });
        },
      });
    });
  });
};

// 获取GPS
const getGps = (isGps, pu_id) => {
  return new Promise((resolve) => {
    if (null == session) {
      alert("无效的连接");
      return;
    }

    var gps = session.swGetPuChanel(pu_id, 65536);
    if (null == gps) {
      alert("没有该设备通道");
      return;
    }

    if (!isGps) {
      console.log("关闭GPS定位");
      return resolve(gps.swClose());
    }

    gps.swOpen({
      repeat: 10,
      interval: 2000,
      callback: function(options, response) {
        if (response.emms.code == jSW.RcCode.RC_CODE_S_OK && response.gps) {
          // let data = response.gps;
          resolve(response.gps);
        }
      },
    });
  });
};

// 播放视频
const play = (id, div) => {
  return new Promise((resolve) => {
    console.log("pu_id: ", id);
    let chanel = session.swGetPuChanel(id, 0);

    // if (hdlg != 0) {
    //   console.log("关闭上一个通道：" + hdlgNum);
    //   chanel.swClose(hdlgNum);
    // }
    console.log('div:'+div);

    if (chanel) {
      let result = chanel.swOpenEx({
        ismuti: true,
        div,
        prototype: "auto", //rtmp > hls
        callback: async function(options, response, dlghandle) {
            console.log('hg: ');
            console.log(dlghandle);
          // console.log(response.emms.code + "视频打开成功，可以获取URl了");
          // alert(response.emms.code + "视频打开成功，可以获取URl了");
          // chanel.swClose(hdlg)
        //   let url = chanel.swGetUrl();
        //   console.log("url ------------------------------");
        //   console.log(url);
          resolve(dlghandle.hdlg)
        },
        media: 10,
      });
      var url = chanel.swGetUrl();
      console.log("url 11 ------------------------------");
      console.log(url);

      if (result != jSW.RcCode.RC_CODE_S_OK) {
        console.log("打开视频失败: " + result);
      }
    } else {
      console.log("没有该设备通道");
    }
  });
};

// 关闭视频
const colseVideo = (id, hdlg) => {
    return new Promise( res => {
        let chanel = session.swGetPuChanel(id, 0);
        res(chanel.swClose(hdlg))
    } )
}

const Helmet = {
  init,
  getGps,
  play,
  colseVideo,
};

export default Helmet;
